package com.space.school.service.impl;

import com.space.school.dao.UserDao;
import com.space.school.dao.impl.UserDaoImpl;
import com.space.school.domain.User;
import com.space.school.service.UserService;
import com.space.school.utils.PageBean;

import java.util.List;
import java.util.Map;

public class UserServiceImpl implements UserService {
    UserDao userDao = new UserDaoImpl();

    @Override
    public PageBean<User> search(int pageSize, int currentPage, Map<String,String[]> map) {

        //        从第几个数据开始
        int start=(currentPage-1)*pageSize;
        List<User> list=userDao.search(start,pageSize,map);
        PageBean<User> pb=new PageBean<User>();
        pb.setList(list);
//        总数据数
        int count=userDao.count(map);
        pb.setTotalCount(count);
//        每页显示数据数
        pb.setRows(pageSize);
//        当前页
        pb.setCurrentPage(currentPage);
//        总页数
//        int Totalpage=(int)Math.ceil((double)count/pageSize);
        int totalPage =(count%pageSize==0 ? count/pageSize : count/pageSize+1);
        pb.setTotalPage(totalPage);
        return pb;
    }

    @Override
    public void delete(User user) {
        userDao.delete(user);
    }

    @Override
    public List<User> findById(int id) {
        return userDao.findById(id);
    }

    @Override
    public void update(User user) {
        userDao.update(user);
    }

    @Override
    public void updateStatus(int id, int status) {
        if (status == 1){
            status = 0;
        }else {
            status = 1;
        }
        userDao.updateStatus(id,status);
    }
}
